export default class Enemies {
  constructor(scene) {
    this.scene = scene

    //敌人子弹组
    this.enemyFires = this.scene.physics.add.group() //创建子弹组

    //敌人组
    this.enemies = this.scene.physics.add.group() //创建敌人组

    //出生组
    this.bores = this.scene.physics.add.group() //创建出生组

    this.timer = null
  }

  //创建初始敌人
  create_enemy(frame) {
    for (let i = 0; i < 8; i++) {
      this.create_one_enemy(0, 0, frame, i)
    }
    return this.enemies
  }

  //创建单个敌人
  create_one_enemy(x, y, frame, i) {
    let type = Math.floor(i / 4) * 32 + (i % 4) * 2
    //创建单个敌人
    console.log(type)
    let enemy = this.enemies.create(x, y, frame, type)
    enemy.setData('type', type) //设置敌人类型

    //添加动画
    this.update_animation(enemy, frame, type)

    //添加敌人属性
    this.update_enemy_type(enemy)

    this.scene.physics.world.enable(enemy)
    enemy.body.setCollideWorldBounds(true)
    enemy.timeToMove = 0
  }

  //根据敌人类型，赋予不同的属性
  update_enemy_type(enemy) {
    let type = enemy.getData('type')
    switch (type) {
      case 0:
        enemy.setData('speed', 8)
        enemy.setData('timeToFire', 1000)
        enemy.setData('score', 100)
        break
      case 2:
        enemy.setData('speed', 8)
        enemy.setData('timeToFire', 1000)
        enemy.setData('score', 100)
        break
      case 4:
        enemy.setData('speed', 8)
        enemy.setData('timeToFire', 1000)
        enemy.setData('score', 100)
        break
      case 6:
        enemy.setData('speed', 20)
        enemy.setData('timeToFire', 1000)
        enemy.setData('score', 100)
        break
      case 32:
        enemy.setData('speed', 8)
        enemy.setData('timeToFire', 1000)
        enemy.setData('score', 200)
        break
      case 34:
        enemy.setData('speed', 8)
        enemy.setData('timeToFire', 1000)
        enemy.setData('score', 200)
        break
      case 36:
        enemy.setData('speed', 8)
        enemy.setData('timeToFire', 1000)
        enemy.setData('score', 200)
        break
      case 38:
        enemy.setData('speed', 8)
        enemy.setData('timeToFire', 1000)
        enemy.setData('score', 200)
        enemy.setData('hp', 3)
        break
      default:
        break
    }
  }

  //动画管理
  update_animation(enemy, key, type) {
    //判断是否存在动画，存在则删除
    if (enemy.anims.currentAnim) {
      this.delete_animation(enemy)
    }
    enemy.setTexture(key, type)

    enemy.anims.create({
      key: 'up',
      frames: this.scene.anims.generateFrameNumbers(key, { start: type + 0, end: type + 1 }),
      frameRate: 5,
      repeat: -1
    })

    enemy.anims.create({
      key: 'right',
      frames: this.scene.anims.generateFrameNumbers(key, { start: type + 8, end: type + 9 }),
      frameRate: 5,
      repeat: -1
    })

    enemy.anims.create({
      key: 'down',
      frames: this.scene.anims.generateFrameNumbers(key, { start: type + 16, end: type + 17 }),
      frameRate: 5,
      repeat: -1
    })

    enemy.anims.create({
      key: 'left',
      frames: this.scene.anims.generateFrameNumbers(key, { start: type + 24, end: type + 25 }),
      frameRate: 5,
      repeat: -1
    })
  }

  //删除动画
  delete_animation(enemy) {
    enemy.anims.remove('up')
    enemy.anims.remove('right')
    enemy.anims.remove('down')
    enemy.anims.remove('left')
  }

  //根据在场敌人数量，随机创建出生点、生成敌人
  create_bore() {
    let randomNumber = Phaser.Math.RND.integerInRange(1, 100)
    if (randomNumber != 55) {
      return
    }
    if (this.enemies.getChildren().length > 4) {
      return
    } else {
      let xx = Phaser.Math.RND.integerInRange(1, 3) * 152
      let bore = this.bores.get(xx, 10, 'bore')
      this.scene.anims.create({
        key: 'go',
        frames: this.scene.anims.generateFrameNumbers('bore', { start: 0, end: 2 }),
        frameRate: 5,
        repeat: -1
      })
      bore.anims.play('go', true)
      this.timer = this.scene.time.delayedCall(
        2000,
        () => {
          bore.destroy()
          this.create_one_enemy(
            xx,
            10,
            'enemy',
            Phaser.Math.RND.integerInRange(0, Phaser.Math.RND.integerInRange(0, 7))
          )
        },
        [],
        this
      )
    }
  }

  //敌人子弹组
  create_enemyFires() {
    //创建子弹组
    return this.enemyFires
  }

  //敌人行为
  update_enemy() {
    this.enemies.getChildren().forEach(enemy => {
      //获取敌人属性
      let speed = enemy.getData('speed')
      if (this.scene.time.now >= enemy.timeToMove) {
        let go = Math.floor(Math.random() * 4) //随机移动方向

        //增大向下移动的概率
        if (go === 2) {
          go = Math.random() < 0.9 ? 2 : 0
        }

        let velocityX = (go === 1 ? speed : go === 3 ? -speed : 0) * 5
        let velocityY = (go === 0 ? -speed : go === 2 ? speed : 0) * 5
        enemy.setVelocity(velocityX, velocityY) //敌人移动
        let animationKey = ['up', 'right', 'down', 'left']
        enemy.play(animationKey[go]) // 播放相应的动画
        enemy.timeToMove = this.scene.time.now + Math.random() * 2000
        if (Math.random() < 0.5) {
          let xx = enemy.x
          let yy = enemy.y
          this.scene.soundFire.play()
          let fire = this.enemyFires.get(xx, yy, 'bullet', go)
          //添加子弹属性
          fire.setData('type', enemy.getData('type'))
          fire.setVelocity((go === 1 ? 8 : go === 3 ? -8 : 0) * 20, (go === 2 ? 8 : go === 0 ? -8 : 0) * 20)
          // fire.setCollideWorldBounds(true);
        }
      }
    })
  }

  //敌人发射子弹
  fireBullet(enemy) {
    let go = Math.floor(Math.random() * 4) //随机移动方向
    if (Math.random() < 0.5) {
      let xx = enemy.x - 5 + (go === 3 ? 0 : go === 1 ? 10 : 5)
      let yy = enemy.y + (go === 0 ? 0 : go === 2 ? 10 : 5)
      this.scene.soundFire.play()
      let fire = this.enemyFires.get(xx, yy, 'bullet', go)
      fire.setVelocity((go === 1 ? 8 : go === 3 ? -8 : 0) * 20, (go === 2 ? 8 : go === 0 ? -8 : 0) * 20)
      fire.setCollideWorldBounds(true)
    }
  }

  //敌人全部爆炸
  boom_enemy() {
    console.log('清除全部敌人')
    console.log(this.enemies.getChildren())
    this.enemies.getChildren().forEach(enemy => {
      enemy.setVisible(false)
      enemy.destroy()
    })
  }
}
